package com.elontian.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.elontian.cache.util.Cache;
import com.elontian.util.BitMapUtil;
import com.elontian.util.CheckUtil;

/**
 * 
 * 具有缓存功能的imageView
 * 
 * @author 胡松振
 * 
 */

public class CacheImageView extends ImageView {

	private Cache<Bitmap> cache;

	// 注册缓存
	public void setCache(Cache<Bitmap> cache) {
		this.cache = cache;
	}

	public CacheImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public CacheImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public CacheImageView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void setImageBitmap(Bitmap bm) {
		// TODO Auto-generated method stub
		if (cache != null) {
			recyleBitmap();
		}
		super.setImageBitmap(bm);
	}

	private void recyleBitmap() {
		String url = (String) this.getTag();
		if (CheckUtil.isNullString(url)) {
			return;
		}

		Bitmap bitmap = BitMapUtil.getImageViewBitmap(this);
		if (CheckUtil.isNotNullBitmap(bitmap)) {
			cache.addLOneCache(url, bitmap);
		}
	}

}
